package features;


import com.vividsolutions.jts.geom.Dimension;
import dbf.DBFdata;
import dbf.ModeloTabela;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Container;
import java.awt.Frame;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.nio.charset.Charset;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTable;
import javax.swing.JToolBar;
import javax.swing.table.TableModel;
import org.geotools.data.FeatureSource;
import org.geotools.data.FileDataStore;
import org.geotools.data.FileDataStoreFinder;
import org.geotools.data.shapefile.dbf.DbaseFileReader;
import org.geotools.map.DefaultMapContext;
import org.geotools.map.MapContext;
import org.geotools.swing.JMapFrame;
import org.geotools.swing.MapLayerTable;
import org.geotools.swing.data.JFileDataStoreChooser;
import org.geotools.swing.event.MapMouseEvent;
import org.geotools.swing.tool.CursorTool;



/**
 * GeoTools Quickstart demo application. Prompts the user for a shapefile
 * and displays its contents on the screen in a map frame
 *
 * @source $URL: http://svn.osgeo.org/geotools/branches/2.7.x/demo/example/src/main/java/org/geotools/demo/Quickstart.java $
 */
public class Tabela extends JFrame {

static JTable tabelaDBF = new JTable();

    /**
     * GeoTools Quickstart demo application. Prompts the user for a shapefile
     * and displays its contents on the screen in a map frame
     */
    public static void main(String[] args) throws Exception  {
        ///ABRIR O SHAPEFILE

         File file = JFileDataStoreChooser.showOpenFile("shp", null);
        if (file == null) {
            return;
        }

        FileDataStore store = FileDataStoreFinder.getDataStore(file);
        FeatureSource featureSource = store.getFeatureSource();

        // Create a map context and add our shapefile to it
        MapContext map = new DefaultMapContext();
        map.setTitle("Quickstart");
        map.addLayer(featureSource, null);
        ////UTILIZA O FILE do SHP PARA ABRIR O DBF
        String dbf = file.toString();
        dbf= dbf.substring(0, dbf.length() - 3);
        dbf = dbf + "dbf";
        File f = new File (dbf);



///////////////////////ABRIR O DBF



        final JPanel painelDBF = new JPanel();

         
        
        int numeroColunasDBF = DBFdata.numeroColunas(f);
        int numeroLinhasDBF = DBFdata.numeroLinhas(f);
        System.out.println("linhas: " + numeroLinhasDBF + " colunas: "+ numeroColunasDBF);//imprime os indices das colunas

         tabelaDBF.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);// ativa barra de rolagem horizontal
        tabelaDBF.setModel(new ModeloTabela(DBFdata.cabecalhoTabela(f), numeroLinhasDBF, numeroColunasDBF));
        
        //DBFdata.matrizDados(f);
          for (int i = 0; i < numeroLinhasDBF; i++) {

                        for (int j = 0; j <numeroColunasDBF; j++) {

                        //System.out.println("TESTE DO OBJECT na linha: " + i + " com valor: " + aobj[j]);
                        tabelaDBF.setValueAt(DBFdata.matrizDados(f,i,j), i, j);
                        //System.out.println(DBFdata.matrizDados(f,i,j));
                        }
                    }

                    //tabelaDBF.getSelectionModel().setSelectionInterval (1, 1);
                    //System.out.println("teste "+tabelaDBF.getValueAt(1, 1));
       
                     

        JScrollPane scrollPane = new JScrollPane(tabelaDBF);
                    scrollPane.setLocation(0, 0);
                    scrollPane.setSize(painelDBF.getWidth()-2, 97);
                    //Dimension dimensao = new Dimension(400, 98);       //Usado para definir o tamanho visível (sem uso do scroll) da tabela
                    //scrollPane.setPreferredSize(dimensao);
                    painelDBF.add(scrollPane);
                    painelDBF.setVisible(true);
        // JMapFrame show = new JMapFrame( );
        // list layers and set them as visible + selected
        // zoom in, zoom out, pan, show all
        // show.setVisible( true );
        // add(painelDBF);
//                    JFrame frame = new JFrame();
//                   frame.getContentPane().add(painelDBF, "South");
//                    //frame.add(painelDBF);
//                   frame.getContentPane().add(painelDBF);
//                    frame.setSize(450, 70);
//                    frame.setVisible(true);
//                    frame.show();

  Frame frame = new Frame("Container Frame");

  frame.add(painelDBF, BorderLayout.CENTER);
  frame.setSize(400,400);
  frame.setVisible(true);
  frame.addWindowListener(new WindowAdapter(){
  public void windowClosing(WindowEvent we){
  System.exit(0);
  }
  });

                    JMapFrame.showMap(map);



    }


}